package com.qzy.reggie.controller;

import com.qzy.reggie.common.PageResult;
import com.qzy.reggie.common.Result;
import com.qzy.reggie.pojo.Dish;
import com.qzy.reggie.service.Two_DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/dish")
public class Two_DishController {

    @Autowired
    private Two_DishService twoDishService;


    @GetMapping()
    public Result list(Integer page,Integer pageSize,String name){
        PageResult pageResult = twoDishService.queryList(page,pageSize,name);

        return Result.success("查询成功",pageResult);
    }

    @PostMapping()
    public Result save(@RequestBody Dish dish,HttpSession session){

        Long empId = (Long) session.getAttribute("empId");

        // 完善系统信息
        dish.setCreateUser(empId);
        dish.setUpdateUser(empId);
        dish.setCreateTime(LocalDateTime.now());
        dish.setUpdateTime(LocalDateTime.now());

        Integer ret = twoDishService.save(dish);

        if (ret > 0){
            return Result.success("新增成功");
        }else{
            return Result.error("新增失败");
        }
    }

    // http://localhost:8080/Dish/1
    @GetMapping("{id}")
    public Result getDishById(@PathVariable Long id){
        return Result.success(twoDishService.queryById(id));
    }

    @PutMapping()
    public Result updateDish(@RequestBody Dish dish,HttpSession session){
        Long empId = (Long) session.getAttribute("empId");
        dish.setUpdateTime(LocalDateTime.now());
        dish.setUpdateUser(empId);

        Integer ret = twoDishService.update(dish);

        if (ret > 0){
            return Result.success("修改成功");
        }else{
            return Result.error("修改失败");
        }
    }

    @DeleteMapping("{id}")
    public Result delete(@PathVariable Long id){
        Integer ret = twoDishService.deleteById(id);

        if (ret > 0){
            return Result.success("修改成功");
        }else{
            return Result.error("修改失败");
        }
    }
}
